package com.alipay.util;

import org.junit.Test;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeCreateModel;
import com.alipay.api.domain.AlipayTradePayModel;
import com.alipay.api.request.AlipayTradeCreateRequest;
import com.alipay.api.request.AlipayTradePayRequest;
import com.alipay.api.response.AlipayTradeCreateResponse;
import com.alipay.api.response.AlipayTradePayResponse;
import com.alipay.constants.AlipayServiceEnvConstants;

public class TestMain {

	/**
	 * 一次请求，创建双方订单，触发时间为商家扫码后
	 * {"alipay_trade_create_response":{"code":"10000","msg":"Success","out_trade_no":"213123141234","trade_no":"2019031422001437340500543507"},"sign":"lUclhjaTPgJfVDw2xZeKYU12GFovQVRxtStcXFdy4g41dHR5FgzXrvvZ2fdqPtL2TMxiycB3ibur2g/FkVY8pV4BqXqJ3vNRbe4ozcrm+ix0+dYKkctuObebaboOehb0oyc9hc1c1dVeg7VsNTOJ6U7Tu8h7F0jZ3q0lSyWv9TtlPHbrD7mUTC5NDxcK4cfktFX522XtkT8GSVQmV0Ag2l6rHpNLQ4v2KBxGPY1CSXvI4uNNlzGvmRGWx8ZuME1194rQrFcVA7gXLXWWyOuYaLGbcGmE0AgaDcfhlZUK6dQfey0JVGloJHJAaUmH/UfDO1kmHfLhlobdJxj+FwYc1Q=="}
	 */
//	@Test
	public static void tradeCreate(){//
		AlipayClient client = new DefaultAlipayClient(AlipayServiceEnvConstants.ALIPAY_GATEWAY, AlipayServiceEnvConstants.APP_ID,
				AlipayServiceEnvConstants.PRIVATE_KEY, "json", AlipayServiceEnvConstants.CHARSET,
				AlipayServiceEnvConstants.ALIPAY_PUBLIC_KEY, AlipayServiceEnvConstants.SIGN_TYPE);
		AlipayTradeCreateRequest request = new AlipayTradeCreateRequest();
		AlipayTradeCreateModel model = new AlipayTradeCreateModel();
		model.setOutTradeNo("21312314123467");//支付及退款条形码号码，可随机数，不可重复
		model.setSellerId("2088102177312550");//商家uid
		model.setTotalAmount("999999");//金额
		model.setBuyerLogonId("gvlogu2769@sandbox.com");//买家支付宝账号
		model.setSubject("老章的玛莎拉蒂");//iphone7 产品名字
		request.setBizModel(model);
		AlipayTradeCreateResponse response = new AlipayTradeCreateResponse();
		try {
			response = client.execute(request);
			System.out.println(response.getBody());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

	}

	/**
	 * 大额支付需要回调两次及以上，三次报错存数据库
	 * {"alipay_trade_pay_response":{"code":"10000","msg":"Success","buyer_logon_id":"gvl***@sandbox.com","buyer_pay_amount":"99999.99","buyer_user_id":"2088102177637345","buyer_user_type":"PRIVATE","fund_bill_list":[{"amount":"99999.99","fund_channel":"ALIPAYACCOUNT"}],"gmt_payment":"2019-03-14 16:28:07","invoice_amount":"99999.99","out_trade_no":"21312314123","point_amount":"0.00","receipt_amount":"99999.99","total_amount":"99999.99","trade_no":"2019031422001437340500543504"},"sign":"SIlX7zpcbWhgW/UpjZ0Xix2q/b3fbZoOfu8gepNL8eLyIlLSrB/lDvmgqE/VGr7iBQhxbn27K3WWto0wtp+PmrJtgEkTpD+PcGZgKYj6sSeyevfI3WqiRUxuy+1u78zrYav0STfLKCIjxGrOxLTTXENzEdSz5m/N6i/jALLrrNeIah10KzNtujVOD4HxZRObBqrmFCDd02B2SWtJAL6dVv+Kgs/o2SyT5YwoAObNaDMCjeirt6ejBCe/2SrmTpiAx2qriWMdM1nQ9ZIYA1yVQC3GCOQPY3GgNyqwuAXHz7JMR79tMbEvnIK4B5vK3bV+aH7e2/0ASy5vGVrHDbD38A=="}
	 * {"alipay_trade_pay_response":{"code":"10000","msg":"Success","buyer_logon_id":"gvl***@sandbox.com","buyer_pay_amount":"0.99","buyer_user_id":"2088102177637345","buyer_user_type":"PRIVATE","fund_bill_list":[{"amount":"0.99","fund_channel":"ALIPAYACCOUNT"}],"gmt_payment":"2019-03-20 17:09:42","invoice_amount":"0.99","out_trade_no":"2131231412346","point_amount":"0.00","receipt_amount":"0.99","total_amount":"0.99","trade_no":"2019032022001437341000003651"},"sign":"rwtl9CIHbM2U0W3dAFJATPLDmwmDtm0tueJNPJJmR4YfWZOH/dXDOttUuDEUSEzaWp05Hn3UR+W0hFcF4aXHGamD2HkeAT3ouauedKXPl16zQzKZqQY1VvAQ12ZwPWUY0xvSyNatVMcIO6v2nEEjpFKeXFLP+7FarkJnoVrSZpahF1Nkzx4IBSLRKJgIu3rCVbNgz0NVGuXMbM+PpNj5MtElfN7+JHxIEgn/xTTnMME2M1BLOgbOKm4x+d4Bs0R/HZX7Bmv9n7B8Exjiw6MKb5e2nFYPFsmgPSxu1M9g0bAaxihh4GxLEQgJqchdmWrE2ICLdusSwA5QOLXuf9A4Og=="}
	 */
//	@Test
	public static void tradePay(){//
		AlipayClient client = new DefaultAlipayClient(AlipayServiceEnvConstants.ALIPAY_GATEWAY, AlipayServiceEnvConstants.APP_ID,
				AlipayServiceEnvConstants.PRIVATE_KEY, "json", AlipayServiceEnvConstants.CHARSET,
				AlipayServiceEnvConstants.ALIPAY_PUBLIC_KEY, AlipayServiceEnvConstants.SIGN_TYPE);
		AlipayTradePayRequest request = new AlipayTradePayRequest();
		AlipayTradePayModel model = new AlipayTradePayModel();
		model.setOutTradeNo("21312314123467");//支付及退款条形码号码，可随机数
		model.setScene("bar_code");//付款码
		model.setAuthCode("283595041281359807");//289179686864713886付款码数字
		model.setSubject("老章的玛莎拉蒂");////iphone7
		model.setTotalAmount("999999");
		request.setBizModel(model);
		AlipayTradePayResponse response = new AlipayTradePayResponse();
		try {
			response = client.execute(request);
			System.out.println(response.getBody());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
//		tradeCreate();
		tradePay();
	}
}
